package com.dems.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dems.dao.AdministratorDao;
import com.dems.dao.MpDao;
import com.dems.dao.StudentDao;

/**
 * Servlet implementation class ForgetServlet
 */
@WebServlet("/ForgetServlet")
public class ForgetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public ForgetServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String accountId = request.getParameter("accountId");
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		String identity = request.getParameter("identity");
		if (identity.equals("student")) {
			StudentDao studentDao = new StudentDao();
			boolean result = studentDao.forget(accountId,phone,password);
			if(result) {
				response.getWriter().write( "<h1 align='center'><font color='red'>修改密码成功，3秒之后跳转回登录页面</font></h1>"); 
				response.setHeader("refresh","3;url=http://localhost/dems/login.jsp"); 
			}else {
				request.setAttribute("forget", "找回密码失败,请修改填写信息");
				request.getRequestDispatcher("/forget.jsp").forward(request, response);
			}	
		}else if (identity.equals("administrator")){
			AdministratorDao administratorDao = new AdministratorDao();
			boolean result = administratorDao.forget(accountId,phone,password);
			if(result) {
				response.getWriter().write( "<h1 align='center'><font color='red'>修改密码成功，3秒之后跳转回登录页面</font></h1>");
				response.setHeader("refresh","3;url=http://localhost/dems/login.jsp");
			}else {
				request.setAttribute("forget", "找回密码失败,请修改填写信息");
				request.getRequestDispatcher("/forget.jsp").forward(request, response);
			}
		}else {
			MpDao mpDao = new MpDao();
			boolean result = mpDao.forget(accountId,phone,password);
			if(result) {
				response.getWriter().write( "<h1 align='center'><font color='red'>修改密码成功，3秒之后跳转回登录页面</font></h1>");
				response.setHeader("refresh","3;url=http://localhost/dems/login.jsp");
			}else {
				request.setAttribute("forget", "找回密码失败,请修改填写信息");
				request.getRequestDispatcher("/forget.jsp").forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
